package exredis

import (
	"github.com/go-redis/redis"
	"github.com/spf13/viper"
)

var (
	rHost        string
	rPassword    string
	redisOptions redis.Options
	rdsClient    *redis.Client
	val          string
	err          error
)

//读取redis配置信息
func readConfig() {
	rHost = viper.GetString("redis.rHost")
	rPassword = viper.GetString("redis.rPassword")
}

// 链接redis
/*
@return
	rdsClient   redis客户端链接
*/
func Conn() (rdsClient *redis.Client) {
	readConfig()
	redisOptions = redis.Options{
		Addr:     rHost,
		Password: rPassword,
		DB:       0,
	}
	rdsClient = redis.NewClient(&redisOptions)
	return
}

//获取redis值
/*
@params
	rdsClient	redis客户端链接
	key      	redis的键
@return
	val			获取到的值
	err 		错误信息
*/
func Get(rdsClient *redis.Client, key string) (val string, err error) {
	val, err = rdsClient.Get(key).Result()
	return
}

//设置redis值
/*
@params
	rdsClient	redis客户端链接
	key      	redis的键
	val			resis设置的值
@return
	err 		错误信息
*/
func Set(rdsClient *redis.Client, key string, val string) (err error) {
	err = rdsClient.Set(key, val, 0).Err()
	return
}

//删除redis值
/*
@params
	rdsClient	redis客户端链接
	key      	redis的键
@return
	err 		错误信息
*/
func Del(rdsClient *redis.Client, key string) (err error) {
	_, err = rdsClient.Del(key).Result()
	return
}
